package commands;

import entities.*;
import containers.*;

/**
 * Make sure that the id and password are correct
 * 
 * @author eks691
 */
public class LoginCheckCommand extends CommandStatus {
	/** The user that is logging in */
	private Employee employee = null;
	private Student student = null;
	@SuppressWarnings("unused")
	private Class<Manager> manager = null;

	/** UserType is either a Student, Employee or Manager */
	public enum UserType {
		STUDENT, EMPLOYEE, MANAGER
	};

	/**
	 * Check if the id and password are valid for a person.
	 * 
	 * @param id
	 *            the id for a user logging in
	 * @param password
	 *            the password from the user
	 */
	public void login(int id, String password) {
		if (password == null) {
			successful = false;
			return;
		}

		/** check employee container for the person logging in */
		employee = EmployeeContainer.dictionary().get(id);
		if (employee == null) {
			/**
			 * not employee: check the student container for the person logging
			 * in
			 */
			student = StudentContainer.dictionary().get(id);
			if (student == null) {
				/**
				 * not student: check the manager class to see if manager is
				 * logging in
				 */
				if (Manager.id != id) {// if id is not the manager id
					successful = false;
					return;
				}// if password is managers password
				else if (password.equals(Manager.password)) {
					manager = Manager.class;
					successful = true;
				} else { // password failed
					successful = false;
				}
			} else if (password.equals(student.getPassword())) {
				successful = true;
			} else {
				successful = false;
			}
		} else if (password.equals(employee.getPassword())) {
			successful = true;
		} else {
			successful = false;
		}
		return;
	}

	/**
	 * Get the user that logged in
	 * 
	 * @return the user that logged in
	 */
	public Person getUser() {
		if (employee != null) {
			return employee;
		} else if (student != null) {
			return student;
		} else {
			// return manager;
			return null;
		}
	}

	/**
	 * Obtain the type of the user who logged in
	 */
	public UserType getUserType() {

		if (employee != null)
			return UserType.EMPLOYEE;
		else if (student != null)
			return UserType.STUDENT;
		else
			return UserType.MANAGER;
	}
}
